package com.ingenieria;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.util.Log;

public class Utilities {
	/**
	 * @class Utilities 
	 * 
	 * Parse a services plainText 
	 * & Check connection 
	 * & Progress
	 * 
	 *
	 * 
	 */
	static String[][] arr;
	static String[] arrdetalle;
	
	public static String[][] getPlainText(String url) {
		InputStream is = null;
		String result = null;
		URI myURI = null;
		
		try {
			myURI = new URI(url);
		} catch (URISyntaxException e) {
		}
		HttpClient httpClient = new DefaultHttpClient();
		HttpGet getMethod = new HttpGet(myURI);
		HttpResponse webServerResponse = null;
		try {
			webServerResponse = httpClient.execute(getMethod);
		}  catch (IOException e) {
		}
		
		HttpEntity httpEntity = webServerResponse.getEntity();
		if (httpEntity != null) {
			try {
				is = httpEntity.getContent();
				BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
	            StringBuilder sb = new StringBuilder();
	            String line = null;
	            while ((line = reader.readLine()) != null) {
	                    sb.append(line + "\n");
	            }
	            is.close();
	            result=sb.toString();
	            String arrayRegister[]=result.trim().split("%%%"); 
	            String sizeField[] = arrayRegister[0].trim().split("@@@"); 
	            
	            Log.i("arrayRegisterSize", ""+arrayRegister.length);
	            Log.i("sizeFieldSize", ""+sizeField.length);
	            
	            arr = new String[arrayRegister.length][sizeField.length];  
	            for(int i=0; i<arrayRegister.length;i++){
	            		for(int j=0;j<sizeField.length;j++){
	            				String arrayFields[] = arrayRegister[i].trim().split("@@@");
	            				arr[i][j] = arrayFields[j];
	            		}
	            }
			} catch (IllegalStateException e) {
				e.printStackTrace();
			} catch (IOException e) {
			}
		}
		return arr;
	}
	
	
	public static String[][] getText(String url) {
		InputStream is = null;
		String result = null;
		URI myURI = null;
		
		try {
			myURI = new URI(url);
		} catch (URISyntaxException e) {
		}
		HttpClient httpClient = new DefaultHttpClient();
		HttpGet getMethod = new HttpGet(myURI);
		HttpResponse webServerResponse = null;
		try {
			webServerResponse = httpClient.execute(getMethod);
		}  catch (IOException e) {
		}
		
		HttpEntity httpEntity = webServerResponse.getEntity();
		if (httpEntity != null) {
			try {
				is = httpEntity.getContent();
				BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
	            StringBuilder sb = new StringBuilder();
	            String line = null;
	            while ((line = reader.readLine()) != null) {
	                    sb.append(line + "\n");
	            }
	            is.close();
	            result=sb.toString();
	            String arrayRegister[]=result.trim().split("%%%"); 
	            String sizeField[] = arrayRegister[3].trim().split("@@@"); 
	            
	            Log.i("arrayRegisterSize", ""+arrayRegister.length);
	            Log.i("sizeFieldSize", ""+sizeField.length);
	            
	            arr = new String[(arrayRegister.length-3)][sizeField.length];  
	            for(int i=3; i<arrayRegister.length;i++){
	            		for(int j=0;j<sizeField.length;j++){
	            				String arrayFields[] = arrayRegister[i].trim().split("@@@");
	            				arr[i-3][j] = arrayFields[j];
	            		}
	            }
			} catch (IllegalStateException e) {
				e.printStackTrace();
			} catch (IOException e) {
			}
		}
		return arr;
	}
	
	
	public static String[] getTextDetalle(String url) {
		InputStream is = null;
		String result = null;
		URI myURI = null;
		
		try {
			myURI = new URI(url);
		} catch (URISyntaxException e) {
		}
		HttpClient httpClient = new DefaultHttpClient();
		HttpGet getMethod = new HttpGet(myURI);
		HttpResponse webServerResponse = null;
		try {
			webServerResponse = httpClient.execute(getMethod);
		}  catch (IOException e) {
		}
		
		HttpEntity httpEntity = webServerResponse.getEntity();
		if (httpEntity != null) {
			try {
				is = httpEntity.getContent();
				BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
	            StringBuilder sb = new StringBuilder();
	            String line = null;
	            while ((line = reader.readLine()) != null) {
	                    sb.append(line + "\n");
	            }
	            is.close();
	            result=sb.toString();
	            String arrayRegister[]=result.trim().split("@@@"); 
	            
	            arrdetalle = arrayRegister;
	            	
			} catch (IllegalStateException e) {
				e.printStackTrace();
			} catch (IOException e) {
			}
		}
		return arrdetalle;
	}
	
	
	
	public static boolean isNetwork(){
	    boolean bool = false;
	    HttpGet requestForTest = new HttpGet("http://m.google.com");
	        try {
	            new DefaultHttpClient().execute(requestForTest); 
	            bool = true;
	        } catch (Exception e) {}
	return bool;
	    }
}
